package com.syuchen.client.udp;

import com.alibaba.fastjson.JSON;
import com.syuchen.client.Client;
import com.syuchen.common.Invocation;
import com.syuchen.common.URL;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UdpClient implements Client {

    @Override
    public Object send(URL url, Invocation invocation) throws Exception {
        DatagramSocket datagramSocket = UdpSocketPool.getDatagramSocket(url);
        String msg = JSON.toJSONString(invocation);
        DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(),
                msg.getBytes().length, InetAddress.getByName(url.getHostname()), url.getPort());
        datagramSocket.send(datagramPacket);
        return null;
    }
}
